//
//  MXYLuckymoneyViewModel.m
//  meiyinxiang
//
//  Created by apple on 2017/10/20.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MXYLuckymoneyViewModel.h"
@implementation MXYLuckymoneyViewModel
+ (MXYLuckymoneyViewModel *)initWithNearbyLuckyMoneyModel:(NearbyLuckyMoneyModel *)model{
    MXYLuckymoneyViewModel *viewModel = [[self alloc] init];
    viewModel.LuckyMoneyId = model.LuckyMoneyId;
    NSString *lat = [NSString stringWithFormat:@"%.5f",model.latitude];
    NSString *lon = [NSString stringWithFormat:@"%.5f",model.longitude];
    viewModel.latitude = lat.floatValue;
    viewModel.longitude = lon.floatValue;
     viewModel.userface = [NSString stringWithFormat:@"%@%@",MYXIMGBaseUrl,model.userface];
    //viewModel.userface = model.userface;
    viewModel.username = model.username;
    viewModel.is_mine = model.is_mine;
    //红包图片判断
    if ([model.is_pick isEqualToString:@"1"]) {
        viewModel.luckyMoneyImg = [UIImage imageNamed:@"recievedMoney"];
    }else{
        viewModel.luckyMoneyImg = [UIImage imageNamed:@"media_luckymoney"];
    }
    if ([model.is_mine isEqualToString:@"1"]) {//我发的红包
        viewModel.luckyMoneyImg = [UIImage imageNamed:@"selfMoney"];
    }
    viewModel.page = 1;
    return viewModel;
}
- (void)setViewModel:(MYXDetailViewModel<Optional> *)viewModel{
    _viewModel = viewModel;
}
- (void)getMoneyDetailWithIsPullup:(BOOL)isPullup Completion:(finishBlock)completion{
    if (isPullup) {
        self.page ++;
    }else{
        self.page = 1;
    }
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [[Http sharedInstance] requestForMethod:GET Url:MYXLuckyMoneyDetail withParams:@{@"id":self.LuckyMoneyId,@"page":page,@"size":@"5"} completion:^(id data, NSError *error) {
        
        if (data) {
            int  nRet = [[data objectForKey:@"status"] intValue];
            NSString *message = data[@"message"];
            if (nRet == 1) {
                NSMutableArray *list = [NSMutableArray array];
                NSError *err = nil;
                NSMutableArray *dataArray = data[@"data"][@"list"];
                list = [NearbyLuckyMoneyDetail arrayOfModelsFromDictionaries:dataArray error:&err];
                NearbyluckyMoneyDetailList *model = [[NearbyluckyMoneyDetailList alloc]initWithDictionary:data[@"data"] error:&err];
                MYXDetailViewModel *detailVM = [MYXDetailViewModel initWithNearLuckyListModel:model];
                
                self.viewModel = detailVM;
                
                completion(YES,message);
            }else{
                completion(NO,message);
            }
        }else{
           // completion(NO,@"请求出错");
        }
    }];
}
@end
